package com.powernode.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysUser;
import com.powernode.model.Result;
import com.powernode.service.SysUserService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Api(tags = "系统用户接口管理")
@RequestMapping("sys/user")
@RestController
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;
    /*
     * 获取当前用户的信息
     * @author RenBoQing
     * @date 2024/7/8 20:17
     * @return com.powernode.model.Result<com.powernode.domain.SysUser>
     */
    @ApiOperation("根据用户ID查询用户信息")
    @GetMapping("/info")
    public Result<SysUser> loadSysUserInfo() {
        // 获取用户ID
        Long userId = AuthUtils.getLoginUserId();
        // 根据用户ID查询用户信息
        SysUser sysUser = sysUserService.getById(userId);
        return Result.success(sysUser);
    }
    /*
     * 多条件分页查询管理员列表
     * @author RenBoQing
     * @date 2024/7/9 19:21
     * @param current
     * @param size
     * @param username
     * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.SysUser>>
     */
    @ApiOperation("多条件分页查询管理员列表")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public Result<Page<SysUser>> loadSysUserPage(@RequestParam(defaultValue = "1") Long current,
                                                 @RequestParam(defaultValue = "10") Long size,
                                                 @RequestParam(required = false) String username) {
        Page<SysUser> page = new Page<>(current, size);
        page = sysUserService.page(page, new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(username), SysUser::getUsername, username)
                .orderByDesc(SysUser::getCreateTime)
        );
        return Result.success(page);
    }

    /*
     * 获取当前用户的个人信息
     * @author RenBoQing
     * @date 2024/7/9 22:21
     * @param userId
     * @return com.powernode.model.Result<SysUserDetailVo>
     */
    @ApiOperation("查询管理员详情(包含角色ID)")
    @GetMapping("info/{userId}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserDetail(@PathVariable Long userId) {
        // 根据管理员ID查询管理员详情
        SysUser sysUser = sysUserService.querySysUserInfoByUserId(userId);
        return Result.success(sysUser);
    }
    /*
     * 修改管理员信息
     * @author RenBoQing
     * @date 2024/7/10 22:16
     * @param sysUser
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUser(@RequestBody SysUser sysUser) {
        Integer count = sysUserService.modifySysUser(sysUser);
        return Result.handle(count > 0);
    }

    /*
     * 新增管理员
     * @author RenBoQing
     * @date 2024/7/10 22:17
     * @param sysUser
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("新增管理员")
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result<String> saveSysUser(@RequestBody SysUser sysUser) {
        Integer result = sysUserService.saveSysUser(sysUser);
        return Result.handle(result > 0);
    }
    /*
     * 删除管理员
     * @author RenBoQing
     * @date 2024/7/10 22:17
     * @param userId
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("批量/单个删除管理员")
    @DeleteMapping("/delete/{userIds}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> deleteSysUsers(@PathVariable List<Long> userIds) {
        Boolean result = sysUserService.removeSysUserListByUserIds(userIds);
        return Result.handle(result);
    }
}